/**
 * jims
 */
package com.jims.master.modules.csm.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.csm.dao.ExpInfoDao;
import com.jims.master.modules.csm.dao.ExpPriceDao;
import com.jims.master.modules.csm.entity.ExpInfo;
import com.jims.master.modules.csm.entity.ExpPrice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 消耗品基本信息Service
 *
 * @author CTQ
 * @version 2017-03-02
 */
@Service
@Transactional(readOnly = true)
public class ExpInfoService extends CrudService<ExpInfoDao, ExpInfo> {
    @Autowired
    private ExpPriceDao expPriceDao;

    public ExpInfo get(String id) {
        return super.get(id);
    }

    /**
     * 添加价表数据自动补全过滤已添加过的消耗品
     *
     * @param expInfo
     * @param orgId
     * @return
     */
    public List<ExpInfo> findAutoCompleteList(ExpInfo expInfo, String orgId, String q, String limit) {
        return dao.findAutoCompleteList(expInfo, orgId, q, limit);
    }

    /**
     * 消耗品入库自动补全已存在价格的消耗品
     * @param expInfo
     * @param orgId
     * @return
     */
    public List<ExpInfo> findAutoCompletePriceist(ExpInfo expInfo, String orgId, String q, String limit) {
        List<ExpInfo> list = dao.findAutoCompletePriceList(expInfo, orgId, q, limit);
        if(null != list && list.size() > 0){
            for (ExpInfo info : list) {
                if(info.getPackageSpec().trim().equals(info.getMinSpec())){   //包装规格 == 规格,说明是小包装,那么包装数量packageNum就是1
                    info.setPackageNum(1.0);
                }
            }
        }
        return list;
    }

    public Page<ExpInfo> findPage(Page<ExpInfo> page, ExpInfo expInfo) {
        return super.findPage(page, expInfo);
    }

    /**
     * 消耗品基本信息保存(expInfo)
     * @param expInfo
     * @return
     * @author fengyg
     * @date 2017-07-10
     */
    @Transactional(readOnly = false)
    public int save(ExpInfo expInfo) {
        if(!"".equals(expInfo.getId().trim())){
            return dao.update(expInfo);
        }
        List<ExpInfo> list = dao.getInfoList(expInfo);
        if(null != list && list.size() > 0){
            return -1;
        }
        return super.save(expInfo);
    }

    @Transactional(readOnly = false)
    public int delInfo(ExpInfo expInfo) {
        int num = 0;
        ExpPrice dp = new ExpPrice();
        dp.setExpInfo(expInfo);
        List<ExpPrice> list = expPriceDao.findByExpInfo(dp);
        if (list != null && list.size() > 0) {
            num = -1;
        } else {
            num = dao.delete(expInfo);
        }
        return num;
    }

    /**
     * 用消耗品类别、消耗品名称查询消耗品基本信息
     *
     * @param expInfo
     * @return
     */
    public Page<ExpInfo> findExpInfoList(Page<ExpInfo> page, ExpInfo expInfo) {
        expInfo.updateOrgId();
        expInfo.setPage(page);
        page.setList(dao.findExpInfoList(expInfo));
        return page;
    }
}